package cn.tedu.ivos2.audit.service.impl;

import cn.tedu.ivos2.application.mapper.ApplicationMapper;
import cn.tedu.ivos2.application.pojo.entity.Application;
import cn.tedu.ivos2.audit.mapper.AuditMapper;
import cn.tedu.ivos2.audit.pojo.dto.AuditQuery;
import cn.tedu.ivos2.audit.pojo.dto.AuditSaveParam;
import cn.tedu.ivos2.audit.pojo.entity.Audit;
import cn.tedu.ivos2.audit.pojo.vo.AuditVO;
import cn.tedu.ivos2.audit.service.AuditService;
import cn.tedu.ivos2.base.enums.ApplicationStatusEnum;
import cn.tedu.ivos2.base.enums.AuditStatusEnum;
import cn.tedu.ivos2.user.mapper.UserMapper;
import cn.tedu.ivos2.user.pojo.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.StringJoiner;

@Transactional
@Slf4j
@Service
public class AuditServiceImpl implements AuditService {
    @Autowired
    private AuditMapper auditMapper;
    @Autowired
    private UserMapper usermapper;
    @Autowired
    private ApplicationMapper applicationMapper;

    @Override
    public void insertAudit(Application application) {
        log.debug("为申请单创建对应的审批单业务:application={}",application);
        List<Long> auditUserIdList = application.getAuditUserIdList();
        for(Long auditUserId:auditUserIdList){
            Audit audit = new Audit();
            audit.setAuditUserId(auditUserId);
            audit.setApplicationId(application.getId());
            if(auditUserIdList.size()>1){
                audit.setAuditStatus(AuditStatusEnum.PENDING.getCode());
            }else{
                audit.setAuditStatus(AuditStatusEnum.MY_PENDING.getCode());
            }
            audit.setCreateTime(new Date());
            auditMapper.insert(audit);
        }
    }

    @Override
    public List<AuditVO> selectAudit(AuditQuery auditQuery) {
        log.debug("查询审批单业务:auditQuery={}",auditQuery);
        List<AuditVO> list = auditMapper.selectAudit(auditQuery);
        for(AuditVO auditVO:list){
            assignAuditUserList(auditVO);
        }
        return list;
    }

    private void assignAuditUserList(AuditVO auditVO){
        List<Long> auditUserIdList = new ArrayList<>();
        List<String> auditUsernameList = new ArrayList<>();
        List<AuditVO> auditVOList = auditMapper.selectAuditByApplicationId(auditVO.getApplicationId());
        for(AuditVO auditVO1 : auditVOList){
            auditUserIdList.add(auditVO1.getAuditUserId());
            UserVO userVO = usermapper.selectById(auditVO1.getAuditUserId());
            auditUsernameList.add(userVO.getUsername());
        }
        StringJoiner sj = new StringJoiner(",");
        for(String username:auditUsernameList){
            sj.add(username);
        }
        auditVO.setAuditUserIdList(auditUserIdList);
        auditVO.setAuditUsernameList(sj.toString());
    }

    @Override
    public void updateAudit(AuditSaveParam auditSaveParam) {
        log.debug("修改审批单业务:auditSaveParam={}",auditSaveParam);
        //定义审批单对象来接受前端传来的数据
        Audit audit = new Audit();
        BeanUtils.copyProperties(auditSaveParam,audit);
        audit.setUpdateTime(new Date());
        //找到当前审批单的申请单对象
        Application application = new Application();
        application.setId(audit.getApplicationId());
        application.setUpdateTime(new Date());
        //判断前端传出来的审批状态是审核通过还是驳回,任何分别进行处理
        if(audit.getAuditStatus().equals(AuditStatusEnum.AUDITED.getCode())){
            auditMapper.update(audit);
            AuditQuery auditQuery = new AuditQuery();
            auditQuery.setApplicationId(audit.getApplicationId());
            Integer count = auditMapper.selectRestAuditCount(auditQuery);
            if(count>0){
                auditQuery.setAuditSort(audit.getAuditSort()+1);
                List<AuditVO> auditVOList = auditMapper.selectAudit(auditQuery);
                if(auditVOList!=null && auditVOList.size()>0){
                    AuditVO auditVO = auditVOList.get(0);
                    Audit audit2 = new Audit();
                    audit2.setId(auditVO.getId());
                    audit2.setAuditStatus(AuditStatusEnum.MY_PENDING.getCode());
                    auditMapper.update(audit2);
                }
            }else{
                application.setStatus(ApplicationStatusEnum.AUDITED.getCode());
                auditMapper.update(audit);
            }
        } else if(audit.getAuditStatus().equals(AuditStatusEnum.REJECT.getCode())){
            auditMapper.update(audit);
            AuditQuery auditQuery = new AuditQuery();
            auditQuery.setApplicationId(audit.getApplicationId());
            List<AuditVO> auditVOList = auditMapper.selectAudit(auditQuery);
            if(auditVOList!=null && auditVOList.size()>0){
                for(AuditVO auditVO:auditVOList){
                    if(AuditStatusEnum.PENDING.getCode().equals(auditVO.getAuditStatus())){
                        auditMapper.deleteById(auditVO.getId());
                    }
                }
            }
            application.setStatus(ApplicationStatusEnum.REJECT.getCode());
            application.setRejectReason(auditSaveParam.getRejectReason());
            applicationMapper.update(application);
        }
    }
}
